package com.example.demo.t2.monad;

import java.util.stream.Stream;

public class Event<T> {
    T data;
    public Event(T data){
        this.data = data;
    }

    @FunctionalInterface
    interface FN<A, B> {
        B apply(A a);
    }

    static class EventData{
        Integer id;
        String msg;
        public EventData(Integer id, String msg) {
            this.id = id;
            this.msg = msg;
        }

        @Override
        public String toString() {
            return "EventData{" +
                    "id=" + id +
                    ", msg='" + msg + '\'' +
                    '}';
        }
    }

    static class Transform {
        static EventData transform(Integer id) {
            switch (id) {
                case 0:
                    return new EventData(id, "零");
                case 1:
                    return new EventData(id, "壹");
                case 2:
                    return new EventData(id, "贰");
                case 3:
                    return new EventData(id, "叁");
                case 4:
                    return new EventData(id, "肆");
                default:
                    return new EventData(5, "伍");
            }
        }
    }

    <B> Event<?> map(FN<T, B> fn) {
        return new Event(fn.apply(this.data));
    }


    public static void main(String[] args) {
        Stream<Event<Integer>> s = Stream.of(
                new Event(1),
                new Event(2),
                new Event(3),
                new Event(4),
                new Event(5)
        );
        s.map(o -> o.map(Transform::transform))
                .forEach(o -> System.out.println(o.data));

    }
}
